
package com.astrocsr.dao;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.astrocsr.common.db.SearchQueryTemplate;
import com.astrocsr.domain.bean.TblParameters;
import com.astrocsr.domain.db.SearchResult;

@Repository("parametersDAO")
public class ParametersDAO extends BaseHelperDAO {

	public TblParameters getTblParameters(TblParameters tblParameters) throws Exception {

		TblParameters result = null; 
		SearchQueryTemplate searchQueryTemplate = new SearchQueryTemplate();
		searchQueryTemplate.setQueryString("FROM TblParameters");
		
		// Set condition (where statement, default logic operator is AND)
		if (tblParameters != null) {
			if (StringUtils.isNotBlank(tblParameters.getParamType())) {
				searchQueryTemplate.setExpression("paramType = :paramType");
				searchQueryTemplate.setParameter("paramType", tblParameters.getParamType());
			}

			if (StringUtils.isNotBlank(tblParameters.getParamCode())) {
				searchQueryTemplate.setExpression("paramCode = :paramCode");
				searchQueryTemplate.setParameter("paramCode", tblParameters.getParamCode());
			}
		}
		
		// execute search with unique result
		result = this.uniqueResult(searchQueryTemplate);
		
		return result;
	} 
	
	public List<TblParameters> listTblParameters(TblParameters tblParameters) throws Exception {

		SearchResult<TblParameters> result = null; 
		String orderColumn = "paramName";
		SearchQueryTemplate searchQueryTemplate = new SearchQueryTemplate(orderColumn, null);
		searchQueryTemplate.setQueryString("FROM TblParameters");
		
		// Set condition (where statement, default logic operator is AND)
		if (tblParameters != null) {
			if (StringUtils.isNotBlank(tblParameters.getParamType())) {
				searchQueryTemplate.setExpression("paramType = :paramType");
				searchQueryTemplate.setParameter("paramType", tblParameters.getParamType());
			}

			if (StringUtils.isNotBlank(tblParameters.getParamCode())) {
				searchQueryTemplate.setExpression("paramCode = :paramCode");
				searchQueryTemplate.setParameter("paramCode", tblParameters.getParamCode());
			}
		}
		
		// execute search with paging
		result = this.find(searchQueryTemplate);
		
		return result;
	} 

	public void updateTblParameters(TblParameters tblParameters) throws Exception {

		String queryString = "update TblParameters model set model.paramValue=:paramValue, model.createdBy=:createdBy, model.createdDate=:createdDate where model.paramType=:paramType and model.paramCode=:paramCode";
		Query query = getCurrentSession().createQuery(queryString);
		query.setParameter("paramValue", tblParameters.getParamValue());
		query.setParameter("createdBy",  tblParameters.getCreatedBy());
		query.setParameter("createdDate", tblParameters.getCreatedDate());
		
		query.setParameter("paramType", tblParameters.getParamType());
		query.setParameter("paramCode", tblParameters.getParamCode());
		query.executeUpdate();
	}


	public void saveTblParameters(TblParameters entity) {
		// Insert to database
		getCurrentSession().save(entity);
	}

}
